package gui.VPOfOperations;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import clients.RPCClient;
import eu.hansolo.steelseries.gauges.Radial;
import eu.hansolo.steelseries.tools.BackgroundColor;
import eu.hansolo.steelseries.tools.ForegroundType;
import eu.hansolo.steelseries.tools.FrameDesign;
import eu.hansolo.steelseries.tools.KnobStyle;
import eu.hansolo.steelseries.tools.KnobType;
import eu.hansolo.steelseries.tools.LedColor;
import eu.hansolo.steelseries.tools.PointerType;
import gui.engraving.EngravingFrame;
import gui.mpm.MPMFrame;

import java.awt.SystemColor;

/**
 * @author Idan
 *	Main class for the VPOP GUI
 */
public class VPOfOperationsMain
{

	private static VPOfOperationsFrame myFrame;
	private static VPOfOperationsDashboard myDashboard;
	public static RPCClient voopClient;
	private static VPOfOperationsTabbedPane myTabbedPane;
	public static Radial dashboardEngraveGauge;
	public static Radial dashboardPolishGauge;
	public static Radial dashboardQcGauge;
	public static Radial dashboardPackagingGauge;
	private static VPOfOperationsReports myReportsPanel;

	
	
    private static void dashboardThread() 
	{
       // Here I create the dashboard panel
        
        final Radial engraveGauge = new Radial();
        final Radial polishGauge = new Radial();
        final Radial qcGauge = new Radial();
        final Radial packagingGauge = new Radial();
        dashboardEngraveGauge = engraveGauge;
        dashboardPolishGauge = polishGauge;
        dashboardQcGauge = qcGauge;
        dashboardPackagingGauge = packagingGauge;
        
        engraveGauge.setTitle("Hanita");
        engraveGauge.setUnitString("Lenses");
        engraveGauge.setFrameDesign(FrameDesign.BLACK_METAL);
        engraveGauge.setBackgroundColor(BackgroundColor.BEIGE);
        engraveGauge.setKnobStyle(KnobStyle.BLACK);
        engraveGauge.setTrackStart(0);
        engraveGauge.setTrackStartColor(Color.RED);
        engraveGauge.setTrackStop(100);
        engraveGauge.setTrackStopColor(Color.GREEN);
        engraveGauge.setTrackVisible(true);
        engraveGauge.setThresholdBehaviourInverted(true);
        engraveGauge.setThreshold(80);
        
    

        polishGauge.setTitle("Hanita");
        polishGauge.setUnitString("Lenses");
        polishGauge.setFrameDesign(FrameDesign.BLACK_METAL);
        polishGauge.setBackgroundColor(BackgroundColor.BEIGE);
        polishGauge.setKnobStyle(KnobStyle.BLACK);
        polishGauge.setTrackStart(0);
        polishGauge.setTrackStartColor(Color.RED);
        polishGauge.setTrackStop(100);
        polishGauge.setTrackStopColor(Color.GREEN);
        polishGauge.setTrackVisible(true);
        polishGauge.setThresholdBehaviourInverted(true);
        polishGauge.setThreshold(80);
        
        qcGauge.setTitle("Hanita");
        qcGauge.setUnitString("Lenses");
        qcGauge.setFrameDesign(FrameDesign.BLACK_METAL);
        qcGauge.setBackgroundColor(BackgroundColor.BEIGE);
        qcGauge.setKnobStyle(KnobStyle.BLACK);
        qcGauge.setTrackStart(0);
        qcGauge.setTrackStartColor(Color.RED);
        qcGauge.setTrackStop(100);
        qcGauge.setTrackStopColor(Color.GREEN);
        qcGauge.setTrackVisible(true);
        qcGauge.setThresholdBehaviourInverted(true);
        qcGauge.setThreshold(80);
        
        packagingGauge.setTitle("Hanita");
        packagingGauge.setUnitString("Lenses");
        packagingGauge.setFrameDesign(FrameDesign.BLACK_METAL);
        packagingGauge.setBackgroundColor(BackgroundColor.BEIGE);
        packagingGauge.setKnobStyle(KnobStyle.BLACK);
        packagingGauge.setTrackStart(0);
        packagingGauge.setTrackStartColor(Color.RED);
        packagingGauge.setTrackStop(100);
        packagingGauge.setTrackStopColor(Color.GREEN);
        packagingGauge.setTrackVisible(true);
        packagingGauge.setThresholdBehaviourInverted(true);
        packagingGauge.setThreshold(80);
        
        
      
        myDashboard.engraveGaugePanel.add(engraveGauge, BorderLayout.CENTER);
        myDashboard.polishGaugePanel.add(polishGauge, BorderLayout.CENTER);
        myDashboard.qcGaugePanel.add(qcGauge, BorderLayout.CENTER);
        myDashboard.packagingGaugePanel.add(packagingGauge, BorderLayout.CENTER);
        
        
        ImageIcon dashboard = new ImageIcon(EngravingFrame.class.getResource("/graphics/dashboard.png"));
        ImageIcon reportsTab = new ImageIcon(EngravingFrame.class.getResource("/graphics/reportsTab.png"));
       
        myTabbedPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        myTabbedPane.addTab("Dashboard",dashboard,myDashboard);
        myTabbedPane.addTab("דוחות", reportsTab, myReportsPanel);
        
    
    }

    public static void main(String[] args)
    {
    	
    	
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MPMFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MPMFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MPMFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MPMFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
       
        
        /* Create client and connect to the Server*/

        
            try
            {
               voopClient = new RPCClient();
               if  (voopClient== null)
            	   System.exit(0);
            

               // get list of orders
           }
           catch  (Exception e) 
           {
             //e.printStackTrace();
           }
            
            
            
            
            
            
    	 final VPOfOperationsFrame frame = new VPOfOperationsFrame();
         myFrame = frame;

         myTabbedPane = new VPOfOperationsTabbedPane();
         myTabbedPane.setBackground(new Color(173, 216, 230));

         
         VPOfOperationsDashboard dashboard = new VPOfOperationsDashboard();
         dashboard.engraveGaugePanel.setBackground(new Color(173, 216, 230));
         dashboard.polishGaugePanel.setBackground(new Color(173, 216, 230));
         dashboard.qcGaugePanel.setBackground(new Color(173, 216, 230));
         dashboard.packagingGaugePanel.setBackground(new Color(173, 216, 230));
         dashboard.dashboardPanel.setBackground(new Color(173, 216, 230));
         dashboard.setBackground(new Color(173, 216, 230));
         myDashboard = dashboard;
         myReportsPanel = new VPOfOperationsReports(myTabbedPane);
         
         
         
         myFrame.setContentPane(myTabbedPane);
         
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {

            	 ArrayList<Object> response = initializeAllFields(dashboard.getDatesForDashboard());
         
	            if (response.get(0).toString().equalsIgnoreCase("Valid"))
	            {		
	            		dashboardThread();
	                    dashboard.setVariables((ArrayList<Object>)response.get(1)); 
	                    myFrame.setSize(1000,1000);
	                    myFrame.pack();
	                    myFrame.setVisible(true);
	               
	            }
	            else
	            	// Failed to get list
            		JOptionPane.showMessageDialog(frame,
						    "שגיאה בחיבור לשרת","שגיאה",JOptionPane.ERROR_MESSAGE);
	            	System.out.println("Something went wrong. " + response.get(0).toString());
	           
	                //set visible
	            
            
            }
        });
        
        
       
        
        
        
        
    }

    /**
     * Initialize all GUI panels
     */
	private static ArrayList<Object> initializeAllFields(ArrayList<Object> datesArrayList)
	{
		
		Timestamp startDate = new Timestamp ( ((Date)datesArrayList.get(0)).getTime() );
		Timestamp endDate = new Timestamp ( ((Date)datesArrayList.get(1)).getTime() );
		Timestamp yesterday = new Timestamp ( ((Date)datesArrayList.get(2)).getTime() );
	    ArrayList<Object> message = new ArrayList<Object>();
	    
	    
	     
	     message.add("initializeAllFieldsVPOP");
	     message.add(startDate);
	     message.add(endDate);
	     message.add(yesterday);
	     
	    
	    // add the dates for the dashboard
	    
	    
	    try 
	    {
	    	
	    	ArrayList<Object> response = voopClient.call(message);
	 
	  
	        return  response; 
	    } catch (Exception ex) {
	        Logger.getLogger(EngravingFrame.class.getName()).log(Level.SEVERE, null, ex);
	    }
	    
	   ArrayList<Object> failed = new ArrayList<Object>();
	   failed.add("initializeAllFieldsEngraving");
	   failed.add("Failed in initializeAllFieldsEngraving");
	    
	      return failed;
	}
}
